package com.bc.trade.config;

import com.bc.trade.model.WxClear;
import com.bc.trade.noitfy.NotifyThread;
import com.bc.trade.service.QueryTask;
import com.bc.trade.service.WxSettlementQuery;
import com.fs.common.util.FSThreadPoolExecutor;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.json.FastJsonFactory;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.ModelRecordElResolver;
//import com.jfinal.plugin.cron4j.Cron4jPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;

/**
 * 整个系统配置
 * 
 * @date 2015年12月29日
 */
public class SystemConfig extends JFinalConfig {	
	
	public static Prop config = PropKit.use("config.properties");
	public static Prop channelConfig = PropKit.use("channelConfig.properties");
	
	/**
	 * 项目基本配置
	 */
	@Override
	public void configConstant(Constants me) {
		me.setDevMode(config.getBoolean("devModel"));
		me.setJsonFactory(new FastJsonFactory());
		me.setViewType(ViewType.JFINAL_TEMPLATE);
	}

	@Override
	public void afterJFinalStart() {
		super.afterJFinalStart();
		// 设置为True, 则不使用JavaBean的getter方法,而是Model的get方法,主要是多表联查,不存在JavaBean的字段可以使用
		ModelRecordElResolver.setResolveBeanAsModel(true);
		SystemInit.initThreadPoolExecutor();
		NotifyThread.getInstance().start();
	}

	/**
	 * 配置路由规则</br>
	 * 如：IndexController,注册后访问地址为 /index/方法&参数
	 */
	@Override
	public void configRoute(Routes me) {
		me.add(new SystemRoute());
	}

	/**
	 * 配置插件,如数据源
	 */
	@Override
	public void configPlugin(Plugins me) {		
		final Prop db = PropKit.use("db.properties");
		String url = db.get("jdbcUrl");
		String userName = db.get("userName");
		String password = db.get("password");
		int initialSize = db.getInt("initialSize");
		int minIdle = db.getInt("minIdle");
		int maxActive = db.getInt("maxActive");
		boolean showSql = db.getBoolean("showSql");
		//读写数据源
		DruidPlugin trade = new DruidPlugin(url,userName,password);
		trade.set(initialSize, minIdle, maxActive);
		me.add(trade);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(trade);
		arp.setShowSql(showSql);
		//映射实体
		MappingKit.mapping(arp);
		me.add(arp);
		
		String adminUrl = db.get("admin_jdbcUrl");
		String adminUserName = db.get("admin_userName");
		String adminPassword = db.get("admin_password");
		int adminInitialSize = db.getInt("admin_initialSize");
		int adminMinIdle = db.getInt("admin_minIdle");
		int adminMaxActive = db.getInt("admin_maxActive");
		//后台数据源
		DruidPlugin adminDB = new DruidPlugin(adminUrl, adminUserName, adminPassword);
		adminDB.set(adminInitialSize, adminMinIdle, adminMaxActive);
		me.add(adminDB);
		ActiveRecordPlugin adminArp = new ActiveRecordPlugin("admin", adminDB);
		adminArp.setShowSql(showSql);
		MappingKit.mappingAdmin(adminArp);
		me.add(adminArp);
		
		
		//Cron4jPlugin wxgjcp = new Cron4jPlugin();
		//每天00 : 00至5 : 00之间每隔50分钟执行一次
		//wxgjcp.addTask("0,50 0-5 * * *", new WxSettlementQuery());
		
		//test 每隔一分钟执行一次
		//wxgjcp.addTask("* * * * *", new WxSettlementQuery());
		
		//me.add(wxgjcp);
		System.out.println("--------定时任务----------- 微信国际定时任务配置成功-------------------");
		new Thread(new WxSettlementQuery()).start();
		
		
		boolean isRunTask = config.getBoolean("isRunTask");
		if(isRunTask) {
//			Cron4jPlugin cp = new Cron4jPlugin();
//			cp.addTask("*/6 * * * *", new QueryTask());
//			me.add(cp);
//			System.out.println("------------------- 定时任务配置成功-------------------");
		}
	}

	/**
	 * 配置拦截器
	 */
	@Override
	public void configInterceptor(Interceptors me) {
	}

	@Override
	public void configHandler(Handlers me) {

	}

	public void beforeJFinalStop(){		
		NotifyThread.getInstance().stop();
		FSThreadPoolExecutor.shutdown();
	}

	@Override
	public void configEngine(Engine me) {
		
	}
}
